package cn.xsly.ymcc.service.impl;

import cn.xsly.ymcc.domain.MessageBlacklist;
import cn.xsly.ymcc.domain.MessageSms;
import cn.xsly.ymcc.mapper.MessageSmsMapper;
import cn.xsly.ymcc.service.IMessageBlacklistService;
import cn.xsly.ymcc.service.IMessageSmsService;

import cn.xsly.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import java.util.Date;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-10-19
 */
@Service
public class MessageSmsServiceImpl extends ServiceImpl<MessageSmsMapper, MessageSms> implements IMessageSmsService {
    @Autowired
    private IMessageBlacklistService blacklistService;


    @Override
    public void save(String title, String content, String phone) {
        MessageSms messageSms = new MessageSms();
        messageSms.setContent(content);
        messageSms.setPhone(phone);
        messageSms.setSendTime(new Date());
        //messageSms.setUserId(); //TODO
        messageSms.setTitle(title);
        //获取IP
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String remoteAddr = attributes.getRequest().getRemoteAddr();
        messageSms.setIp(remoteAddr);

        super.insert(messageSms);
    }

    @Override
    public void blacklist(MessageSms messageSms) {
        String ip = messageSms.getIp();
        String phone = messageSms.getPhone();
        Boolean aBoolean = blacklistService.selectByIpAndPhone(ip, phone);
        AssertUtil.isTrue(aBoolean, "已经存在黑名单");
        MessageBlacklist blacklist = new MessageBlacklist();
        blacklist.setIp(ip);
        blacklist.setPhone(phone);
        blacklistService.insert(blacklist);
    }

}
